//
//  YNSPayReusableHeadView.m
//  HNBaseProject
//
//  Created by TR on 2024/7/24.
//

#import "YNSPayReusableHeadView.h"

@implementation YNSPayReusableHeadView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor whiteColor];
        self.bgImageV = [[UIImageView alloc]init];
        self.bgImageV.image = [UIImage imageNamed:@"pay_bg"];
        [self addSubview:self.bgImageV];
        [self.bgImageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(20);
            make.left.offset(15);
            make.right.offset(-15);
            make.height.offset(kRealValue(122));
        }];
        
        self.tLab = [[UILabel alloc]init];
        self.tLab.text=@"我的钻石";
        self.tLab.textColor = [UIColor colorWithHexString:@"#ffffff"];
        self.tLab.font = [UIFont systemFontOfSize:16];
        [self addSubview:self.tLab];
        [self.tLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(45);
            make.left.offset(42);
        }];
        
        self.zsImageV = [[UIImageView alloc]init];
        self.zsImageV.image = [UIImage imageNamed:@"pay_zs"];
        [self addSubview:self.zsImageV];
        [self.zsImageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.tLab.mas_bottom).offset(21);
            make.left.offset(42);
            make.width.height.offset(28);
        }];
        
        self.zsLab = [[UILabel alloc]init];
        self.zsLab.text=@"----";
        self.zsLab.textColor = [UIColor colorWithHexString:@"#ffffff"];
        self.zsLab.font = [UIFont systemFontOfSize:37 weight:UIFontWeightBold];
        [self addSubview:self.zsLab];
        [self.zsLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.zsImageV.mas_centerY).offset(0);
            make.left.equalTo(self.zsImageV.mas_right).offset(10);
        }];
        
        self.tipLab = [[UILabel alloc]init];
        self.tipLab.text=@"购买钻石";
        self.tipLab.textColor = [UIColor colorWithHexString:@"#000000"];
        self.tipLab.font = [UIFont systemFontOfSize:18 weight:UIFontWeightBold];
        [self addSubview:self.tipLab];
        [self.tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bgImageV.mas_bottom).offset(30);
            make.left.offset(34);
        }];
        
        self.tipImageV = [[UIImageView alloc]init];
        self.tipImageV.image = [UIImage imageNamed:@"zz_tips"];
        [self addSubview:self.tipImageV];
        [self.tipImageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bgImageV.mas_bottom).offset(42);
            make.left.offset(23);
            make.width.height.offset(19);
        }];
        AIUserInfoModel *model = [RQCacheTool getCurrentUserInfo];
        if (model.token.length > 0) {
            [self getUserMoney];
        }
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadMony) name:@"buysuccess" object:nil];
    }
    return self;
}

//获取余额
- (void)getUserMoney {
    MJWeakSelf
    [[DCHttpClient shareClient] requestWithPath:@"api/user/center" params:@{} httpMethod:DCHttpRequestGet sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary * dict = [responseObject mj_JSONObject];

        if([dict[@"code"] intValue]==1){
            NSString *user_money = [NSString stringWithFormat:@"%@",dict[@"data"][@"user_money"]];
            weakSelf.zsLab.text = user_money;
        } else {

        }
    } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
    }];
}

- (void)reloadMony {
    [self getUserMoney];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
